package org.example.codeservce.util;

import cn.hutool.json.JSONUtil;
import org.example.codeservce.comm.Q;
import org.example.codeservce.entity.FileCode;
import org.example.codeservce.pojo.vo.PromptVo;
import org.example.codeservce.pojo.vo.TreeDTO;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Li Qing
 * date: 2024/6/18 14:43
 * description:
 */
public class TreeDataUtil
{
    public static Q todoTree(FileCode fileCode)
    {
        //配置文件转换
        String resourcesPromptVo = fileCode.getResourcesPromptVo();
        List<PromptVo> resourcesList = JSONUtil.toList(resourcesPromptVo, PromptVo.class);
        List<TreeDTO> resourcesDto = resourcesList.stream().map(resource ->
                                                                        new TreeDTO()
                                                                                .setPath(resource.getPath())
                                                                                .setFileName(resource.getFileName())
                                                                                .setResultPrompt(resource.getResultPrompt())).collect(Collectors.toList());


        //遍历包
        List<TreeDTO> pageInfo = JSONUtil.toList(fileCode.getPageInfos(), TreeDTO.class);
        Map<String, TreeDTO> treeDTOMap = pageInfo.stream().collect(Collectors.toMap(TreeDTO::getFileName, e -> e, (k1, k2) -> k1));
        String promptVo = fileCode.getClassPromptVo();
        Map<String, List<PromptVo>> promptVoList = JSONUtil.toBean(promptVo, Map.class);
        List<TreeDTO> treeDTOS = promptVoList.keySet().stream().map(key ->
                                                                    {
                                                                        if (treeDTOMap.containsKey(key))
                                                                        {
                                                                            return treeDTOMap.get(key)
                                                                                    .setChildren(promptVoList.get(key))
                                                                                    ;
                                                                        }
                                                                        return new TreeDTO().setFileName(key)
                                                                                .setChildren(promptVoList.get(key));
                                                                    }).collect(Collectors.toList());
        return Q.ok().q("resourcesDto", resourcesDto).q("treeDto", treeDTOS);
    }
}
